package com.litchi.activity;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

import com.lib.uil.ToastUtils;
import com.litchi.R;
import com.litchi.adapter.GoodsListAdapter;
import com.litchi.bean.Brands;
import com.litchi.bean.Goods;
import com.litchi.utils.Constants;
import com.litchi.utils.Utils;

public class GoodsListActivity extends Activity {

	private ProgressBar mProgressBar;
	private ListView mListVew;
	private Context mContext;
	private List<Goods> mList;
	private GoodsListAdapter mGoodsListAdapter;
	private Brands brands;
	private String searchWord;
	private ImageView back;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_goods_list);
		mContext = this;
		initView();

		brands = (Brands) getIntent().getSerializableExtra(Constants.BRAND_KEY);
		searchWord = getIntent().getStringExtra(Constants.GOODS_SEARCH_KEY);
		if (null != brands) {
			initBrandGoodsData();
		} else if (!TextUtils.isEmpty(searchWord)) {
			initSearchGoodsData();
		}

	}

	private void initBrandGoodsData() {

		BmobQuery<Goods> query = new BmobQuery<Goods>("Goods");
		query.addWhereEqualTo("brandName", brands.getBrandName());
		query.setLimit(50);
		query.findObjects(mContext, new FindListener<Goods>() {

			@Override
			public void onError(int arg0, String arg1) {
				// TODO Auto-generated method stub
				mProgressBar.setVisibility(View.GONE);
				Utils.showToast(mContext, "请检查网络连接" + arg0);
			}

			@Override
			public void onSuccess(List<Goods> arg0) {
				// TODO Auto-generated method stub
				mProgressBar.setVisibility(View.GONE);
				mListVew.setVisibility(View.VISIBLE);
				mList = arg0;
				mGoodsListAdapter = new GoodsListAdapter(mContext, mList);
				mListVew.setAdapter(mGoodsListAdapter);

			}

		});
	}

	public void initSearchGoodsData() {

		if (!TextUtils.isEmpty(searchWord)) {
			BmobQuery<Goods> query = new BmobQuery<Goods>("Goods");
			query.addWhereContains("goodsName", searchWord.trim());
			query.setLimit(50);
			query.findObjects(mContext, new FindListener<Goods>() {

				@Override
				public void onError(int arg0, String arg1) {
					// TODO Auto-generated method stub
					ToastUtils.showToast(mContext, "发生未知错误");
				}

				@Override
				public void onSuccess(List<Goods> arg0) {
					// TODO Auto-generated method stub
					mProgressBar.setVisibility(View.GONE);
					mListVew.setVisibility(View.VISIBLE);
					if (arg0.size() > 0) {
						
						mList = arg0;
						mGoodsListAdapter = new GoodsListAdapter(mContext, mList);
						mListVew.setAdapter(mGoodsListAdapter);
					} else {
						ToastUtils.showToast(mContext, "未找到相关产品");
					}

				}

			});
		}

	}

	/**
	 * 设置菜单宽度
	 */
	@Override
	protected void onResume() {
		super.onResume();

	}

	private void initView() {
		  back=(ImageView)findViewById(R.id.img_back);
		  back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		mProgressBar = (ProgressBar) findViewById(R.id.list_progressBar);
		mListVew = (ListView) findViewById(R.id.brand_goods_list);
		mListVew.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				gotoGoodsDetail(mList.get(position));
			}
		});
	}

	/**
	 * 跳转商品详情
	 * 
	 * @param info
	 */
	private void gotoGoodsDetail(Goods info) {
		Intent intent = new Intent(this, DetailActivity.class);
		intent.putExtra(Constants.GOODS_KEY, info);
		startActivity(intent);
	}

}
